package com.SouthernWall_404.AgeRight.Common.API.MissionAPI;

import com.SouthernWall_404.AgeRight.Common.AgeRight;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.registries.ForgeRegistries;

import java.util.Objects;

public class KillMissionInfo extends AbstractMissionInfo {

    public EntityType<?> targetType;
    public ResourceLocation targetEntityId;
    public static final String describePosition="gui.mission.kill";

    public KillMissionInfo(int goal, LivingEntity livingEntity) {
        super(goal, MissionType.KILL,describePosition);
        this.targetType = livingEntity.getType();
        this.targetEntityId = ForgeRegistries.ENTITY_TYPES.getKey(targetType);
    }

    // 新增：使用EntityType的构造函数
    public KillMissionInfo(int goal, EntityType<?> entityType) {
        super(goal, MissionType.KILL,describePosition);
        this.targetType = entityType;
        this.targetEntityId = ForgeRegistries.ENTITY_TYPES.getKey(entityType);
    }

    // 无参构造函数用于NBT加载
    public KillMissionInfo() {
        super(0, MissionType.KILL,describePosition);
    }



    @Override
    public void checkKill(LivingEntity livingEntity, Player player) {
        super.checkKill(livingEntity, player);

        // 检查击杀的实体是否与目标实体类型匹配
        if (targetType != null && livingEntity.getType().equals(targetType)) {
            progressOn();
            check(player);
        }
    }

    @Override
    public Component getDescribe() {

        Component entityName = Component.translatable(targetType.getDescriptionId());
        return Component.translatable(describe,getGoal(),entityName,getProgress(),getGoal());
    }

    /**
     * 保存击杀任务的具体内容
     */
    @Override
    protected CompoundTag saveMissionContentPack() {
        CompoundTag contentPack = new CompoundTag();

        // 保存目标实体类型
        if (targetEntityId != null) {
            contentPack.putString("target_entity", targetEntityId.toString());
        }

        return contentPack;
    }

    /**
     * 加载击杀任务的具体内容
     */
    @Override
    protected void loadMissionContentPack(CompoundTag missionContentPack) {
        // 加载目标实体类型
        if (missionContentPack.contains("target_entity")) {
            String entityId = missionContentPack.getString("target_entity");
            this.targetEntityId = new ResourceLocation(entityId);
            this.targetType = ForgeRegistries.ENTITY_TYPES.getValue(targetEntityId);
        }
    }


    /**
     * 检查任务是否有效（目标实体类型是否存在）
     */
    public boolean isValid() {
        return targetType != null && targetEntityId != null;
    }

    /**
     * 设置目标实体类型
     */
    public void setTargetType(EntityType<?> entityType) {
        this.targetType = entityType;
        this.targetEntityId = ForgeRegistries.ENTITY_TYPES.getKey(entityType);
    }

    /**
     * 获取目标实体类型ID
     */
    public ResourceLocation getTargetEntityId() {
        return targetEntityId;
    }
}